// 云函数入口文件
const cloud = require('wx-server-sdk')
// 拓展小程序路由
const TcbRouter = require('tcb-router');
// 异步请求
const rp = require('request-promise');
cloud.init()
const BASE_URL = 'http://musicapi.xiecheng.live'
// 云函数入口函数
exports.main = async(event, context) => {

  const app = new TcbRouter({
    event
  });
  //获取数据列表
  app.router("playlist", async(ctx, next) => {
    let data = await cloud.database().collection("playlist")
      .skip(event.start)
      .limit(event.count)
      .orderBy('createTime', 'desc')
      .get()
      .then(res => {
        return res;
      })
    ctx.body = data;
  })
  //获取歌单列表
  app.router('musiclist', async(ctx, next) => {
    ctx.body = await rp(BASE_URL + '/playlist/detail?id=' + parseInt(event.playlistId)).then(res => {
      return JSON.parse(res);
    })
  })
  //获取歌曲播放url
  app.router('musicPlay',async (ctx, next) =>{
    ctx.body = await rp(BASE_URL + '/song/url?id=' + parseInt(event.musicid) + ',320000').then(res => {
      return JSON.parse(res);
    })
  })
  //获取歌词
  app.router('lyric',async (ctx,next) => {
    ctx.body = await rp(BASE_URL + '/lyric?id=' + parseInt(event.musicid)).then(res => {
      return res;
    })
  })
  //推荐列表
  app.router("story", async(ctx, next) => {
    let data = await cloud.database().collection("yuyin_playList")
      .skip(event.start)
      .limit(event.count)
      .get()
      .then(res => {
        return res;
      })
    ctx.body = data;
  })
  //推荐详情
  app.router("storylist", async(ctx, next) => {
    let data = await cloud.database().collection("yuyin_playItem").where({
      id: parseInt(event.musicid)
    }).get().then(res => res);
    ctx.body = data;
  })
  return app.serve()


}